package cn.jsj.machine.dao;

import cn.jsj.machine.pojo.CMachine;
import org.apache.ibatis.annotations.*;
import org.hibernate.validator.constraints.Length;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Repository
public interface IMachineDao {

    @Results(id = "toMachine", value = {
            @Result(id = true,property = "machineID", column = "machine_id"),
            @Result(property ="machineName", column = "machine_name"),
            @Result(property = "machineValue", column = "machine_value"),
            @Result(property = "machineDescription", column = "machine_des")
    })
    @Select("select * from tb_machine where machine_id = #{machineID} limit 1")
    public CMachine getOneMachineByID(@Param("machineID")@NotNull@Min(value = 1) Integer machineID);

    @Select("select part_number from tb_machine_part where machine_id = #{machineID} and part_id = #{partID}")
    public Integer getPartNeedNumberOfMachine(@Param("machineID")@NotNull@Min(value = 1) Integer machineID,@Param("partID")@NotNull@Min(value = 1) Integer partID);

    @Update("update tb_machine set machine_name = #{newName} where machine_id = #{machineID}")
    public Integer renameOfMachine(@Param("machineID")@NotNull@Min(value = 1) Integer machineID,@Param("newName")@NotBlank@Length(min = 1,max = 20) String newName);

    @ResultMap(value = "toMachine")
    @Select("select * from tb_machine")
    public List<CMachine> getAllOfMachines();

    @Update("update tb_machine set machine_des = #{machineInf} where machine_id = #{machineID}")
    public Integer resetDescriptionOfMachine(@Param("machineID")@NotNull@Min(value = 1) Integer machineID,@Param("machineInf")@NotBlank@Length(max = 225) String machineInf);

    @Update("update tb_machine_part set part_number = #{newNeedNumber} where machine_id = #{machineID} and part_id = #{partID}")
    public Integer changeNeedNumber(@Param("machineID")@NotNull@Min(value = 1) Integer machineID,@Param("partID")@NotNull@Min(value = 1) Integer partID,@Param("newNeedNumber")@NotNull@Min(value = 1) Integer newNeedNumber);

    @Update("update tb_machine set machine_value = machine_value - #{reduceValue} where machine_id = #{machineID}")
    public Integer reduceMachineValue(@Param("machineID") Integer machineID,@Param("reduceValue") Double reduceValue);

    @Update("update tb_machine set machine_value = machine_value + #{addValue} where machine_id = #{machineID}")
    public Integer addMachineValue(@Param("machineID")@NotNull@Min(value = 1) Integer machineID,@Param("addValue")@NotNull@Min(value = 0) Double addValue);

    @Delete("delete from tb_machine where machine_id = #{machine}")
    public Integer deleteMachineInformation(@Param("machine")@NotNull@Min(value = 1) Integer machineID);

    @Delete("delete from tb_machine_part where machine_id = #{machineID} and part_id = #{partID}")
    public Integer deleteOnePartInformationOfMachine(@Param("machineID")@NotNull@Min(value = 1) Integer machineID,@Param("partID")@NotNull@Min(value = 1) Integer partID);

    @Delete("delete from tb_machine_part where machine_id = #{machineID}")
    public Integer deletePartInformationOfMachine(@Param("machineID")@NotNull@Min(value = 1) Integer machineID);

    @ResultMap(value = "toMachine")
    @Select("select tb_machine.* from tb_machine INNER JOIN tb_machine_part on tb_machine.machine_id = tb_machine_part.machine_id where tb_machine_part.part_id = #{partID} GROUP BY machine_id")
    public List<CMachine> getMachineListUseOfPart(@Param("partID")@NotNull@Min(value = 1) Integer partID);

    @Options(useGeneratedKeys = true,keyProperty = "machineID", keyColumn = "machine_id")
    @Insert("insert into tb_machine (machine_name) values (#{machineName})")
    public Integer addOneMachine(@NotNull CMachine machine);

    @ResultMap(value = "toMachine")
    @Select("select * from tb_machine where machine_name = #{machineName} limit 1")
    public CMachine getOneMachineByMachineName(@Param("machineName")@NotBlank@Length(min = 1,max = 20) String machineName);

    @Insert("insert into tb_machine_part (machine_id, part_id, part_number) values (#{machineID},#{partID},#{number})")
    public Integer addOnePartOfMachine(@Param("machineID")@NotNull@Min(value = 1) Integer machineID,@Param("partID")@NotNull@Min(value = 1) Integer partID,@Param("number")@NotNull@Min(value = 1) Integer number);
}
